package com.hmall.user.interceptor;

import com.hmall.user.utils.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component

public class UserHandlerInterceptor implements HandlerInterceptor {
    // 在业务处理器处理请求之前被调用
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String authorization = request.getHeader("authorization");
        System.out.println("请求头:" + authorization);
        if (StringUtils.isBlank(authorization)) {
            System.out.println("被拦截");
            return false;
        }
        BaseContext.setCurrentId(Long.parseLong(authorization));
        return true;
    }
}
